package com.fap.core.bo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.fap.util.StringUtil;

public class BoSqlProvider {

	public String getObject(BusinessObject param){
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT ");
		sql.append(StringUtil.combinStringArray(param.getMeta().getFields(),","));
		sql.append(" FROM ");
		sql.append(param.getMeta().getTableName());
		sql.append(" WHERE ");
		sql.append(param.getMeta().getPrimaryField());
		sql.append("=#{id}");
		
		System.out.println(sql.toString());
		return sql.toString();
	}
	
	public String query(Map<String, Object> params){
		BusinessObject param = (BusinessObject)params.get("param");
		
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT ");
		sql.append(StringUtil.combinStringArray(param.getMeta().getFields(),","));
		sql.append(" FROM ");
		sql.append(param.getMeta().getTableName());
		List<String> conditions = new ArrayList<String>();
		
		for(String field: param.getMeta().getFields()){
			Object value = param.getValue(field);
			if(value!=null){
				conditions.add(field+"=#{param."+field+"}");
			}
		}
		if(conditions.size()>0){
			sql.append(" WHERE ");
			sql.append(StringUtil.combinStringArray(conditions.toArray(new String[conditions.size()]), " and "));
		}
		
		System.out.println(sql.toString());
		return sql.toString();		
	}
		
	public String insert(BusinessObject data){
		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO ");
		sql.append(data.getMeta().getTableName());
		List<String> fields = new ArrayList<String>();
		List<String> values = new ArrayList<String>();
		
		for(String field: data.getMeta().getFields()){
			Object value = data.getValue(field);
			if(value!=null){
				fields.add(field);
				values.add("#{"+field+"}");
			}
		}
		
		sql.append(" ("+StringUtil.combinStringArray(fields.toArray(new String[fields.size()]),",")+") ");
		sql.append(" VALUES ");
		sql.append(" ("+StringUtil.combinStringArray(values.toArray(new String[values.size()]),",")+") ");
		
		System.out.println(sql.toString());
		return sql.toString();
	}
	
	public String update(BusinessObject data){
		String pkField = data.getMeta().getPrimaryField();
		
		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE ");
		sql.append(data.getMeta().getTableName());
		List<String> fields = new ArrayList<String>();
		
		for(String field: data.getMeta().getFields()){
			if(field.equals(pkField))
				continue;
			
			Object value = data.getValue(field);
			if(value!=null){
				fields.add(field+"=#{"+field+"}");
			}
		}
		
		sql.append(" SET "+StringUtil.combinStringArray(fields.toArray(new String[fields.size()]),","));
		
		sql.append(" WHERE ");
		sql.append(pkField);
		sql.append("=#{id}");

		System.out.println(sql.toString());
		return sql.toString();
	}
	
	public String delete(BusinessObject data){
		StringBuffer sql = new StringBuffer();
		sql.append("DELETE FROM ");
		sql.append(data.getMeta().getTableName());
		sql.append(" WHERE ");
		sql.append(data.getMeta().getPrimaryField());
		sql.append("=#{id}");
		
		System.out.println(sql.toString());
		return sql.toString();
	}
}
